package com.shujia.scala

import java.io.{BufferedReader, FileReader}

import scala.io.{BufferedSource, Source}

object Demo6OnJava {
  def main(args: Array[String]): Unit = {

    /**
      * io
      *
      */

    //使用java中的io流读取文件
    //字符流
    val fileReader: FileReader = new FileReader("data/students.txt")

    //字符缓存流
    val bufferedReader: BufferedReader = new BufferedReader(fileReader)

    //读取第一行数据
    var line: String = bufferedReader.readLine()
    //判断是否还有数据
    while (line != null) {

      println(line)

      //读取下一行
      line = bufferedReader.readLine()
    }

    //关闭流
    bufferedReader.close()
    fileReader.close()


    println("=" * 100)

    /**
      * scala自己读取数据的方法
      */


    //底层还是java的io流
    val bufferedSource: BufferedSource = Source.fromFile("data/students.txt")

    //读取所有行,得到一个迭代器
    val lines: Iterator[String] = bufferedSource.getLines()

    for (line <- lines) {
      println(line)
    }


  }

}
